package com.company.demo.adapter_ac_dc;

import com.company.demo.TestInterface;
import com.company.demo.adapter.AdapterObject;

import java.util.ArrayList;
import java.util.List;

public class PowerAdapterTest implements TestInterface {
    List<TargetPower> targetPowerList = new ArrayList<>();

    public PowerAdapterTest() {
        targetPowerList.add(new ChinaPowerAdapter());
        targetPowerList.add(new JapanPowerAdapter());
    }

    public TargetPower getPowerAdapter(AC ac) throws IllegalAccessException {
        for (TargetPower targetPower :
                targetPowerList) {
            if (targetPower.isSupport(ac)) {
                return targetPower;
            }
        }
        throw new IllegalAccessException("没有找到合适的适配器");
    }

    @Override
    public void run() {
        AC ac;

        System.out.println("电源适配器");

        ac = new AC110();
        try {
            TargetPower powerAdapter = getPowerAdapter(ac);
            powerAdapter.outputDC5V();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        ac = new AC220();
        try {
            TargetPower powerAdapter = getPowerAdapter(ac);
            powerAdapter.outputDC5V();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }


    }
}
